<?php
require_once 'BaseController.php';

/**
 * 【SP版】検討リスト コントローラ
 */
class Mobileapi_StudyController extends Mobileapi_BaseController
{

    /**
     * メインアクション
     */
    public function indexAction()
    {
        header("Cache-Control: no-cache, must-revalidate"); // HTTP/1.1
        header("Expires: Sat, 26 Jul 1997 05:00:00 GMT"); // 過去の日付
        $this->doAction();
    }

    /**
     * メインアクション(post)
     */
    public function postAction()
    {
        $this->doAction();
    }

    private function doAction()
    {
        $params = $this->getParameters();
        $kentoList = $_COOKIE['kentoList'];

        if (!isset($kentoList) || empty($kentoList) || $kentoList == '')
        {
            $this->_data['NoData'] = true;
            $this->displayBody2('index');
            return;
        }

        // ページャ情報
        $page = $params['Page'];

        $page = ($page == NULL || $page == '') ? 1 : intval($page);
        if ($page == 0) $page = 1;
        $page_size = $params['page_size'] = 20;
        $pager_from = ($page - 1) * $page_size;
        $pager_to = $pager_from + $page_size - 1;
        // ページャ情報

        $model = new Mobileapi_Model_BukkenList();
        $model_conv = new Rstobi_Model_Converte();
        $model_common = new Rstobi_Model_Common();

        $from = $model_common->getCampFrom($params);

        $dbParams = array();

        $kentos = explode(',', $kentoList);
        $count = sizeof($kentos);
        $this->_data['tatemono_count'] = $count;

        $num_pages = (int) ceil($count / $page_size);
        $this->_data['num_pages'] = $num_pages;

        if ($page > 1) $this->_data['page_prev'] = $page - 1;
        if ($page < $num_pages) $this->_data['page_next'] = $page + 1;
        $this->_data['pagination'] = $this->getPagination($page, $num_pages);
        $this->_data['page'] = $page;

        $from = ($page - 1) * $page_size;
        $to = $pager_from + $page_size - 1;
        if ($to > $count - 1) $to = $count - 1;

        $kentoSearch = array();
        for ($i = $from; $i <= $to; $i++)
        {
            $kentoSearch[] = $kentos[$i];
        }

        $data = $model->getBukenByCds(implode(',', $kentoSearch), $from, $dbParams);
        $model_conv->dataConverteForBuken($data, $params, false, false);

        $prev = NULL;
        $c = -1;
        foreach ($data as $d)
        {
            $tatemono = $d['T03_DAIHYO_TATEMONO_CD'];
            if ($prev != $tatemono)
            {
                $c++;
                $items[] = array();
            }
            $d['MAX_CHINRYO_STR'] = (intval($d['MAX_CHINRYO']) / 10000);
            $d['MIN_CHINRYO_STR'] = (intval($d['MIN_CHINRYO']) / 10000);
            // OEM判定
            $OemFlg = ((array_key_exists('OemCd', $params) == true) && (is_null($params['OemCd']) == false) && (is_numeric($params['OemCd']) == true));
            // 物件画像取得
            $d['BUKEN_IMAGE'] = Apm_Bukkenitems::GetImageDataMain($d, $OemFlg);
            $d['FULLGAZOU_ICON'] = Apm_Bukkenitems::GetFullGazouIcon($d['BUKEN_IMAGE']);
            // キャンペーン取得
            $heyaCd = $d['T03_HEYA_CD'];
            $ret = $model->getCamp($heyaCd);
            if (!empty($ret))
            {
                $d['M94_ITEM_ICON'] = $ret['M94_ITEM_ICON'];
                $d['M94_CAMPAIGN_URL'] = $ret['M94_CAMPAIGN_URL'];
                $d['M94_CAMPAIGN_NAME'] = $ret['M94_CAMPAIGN_NAME'];
            }

            $items[$c][] = $d;
            $prev = $tatemono;
        }

        $this->_data['list'] = $items;

        $this->displayBody2('index');
    }

}
?>